import { defineStore } from 'pinia'
import { useUserStore } from './user'
import axios from '../utils/myAxios'

// 评论类型
export interface CommentType {
  comment_id?: number
  comment_content: string
  comment_time: number
  commenter: string
  message_id: number
}

// 留言类型
export interface MessageType {
  message_id?: number
  author: string
  content: string
  created_at: number
  tag: number
  likes_num?: number
  comments_num?: number
  color: number
  comments?: CommentType[]
}

export const useMessagesStore = defineStore('Messages', {
  state: () => {
    return {
      // 所有的留言
      messages: [] as MessageType[]
    }
  },
  actions: {
    // 请求所有的留言
    async fetchMessages() {
      try {
        const response = await axios({
          method: 'get',
          url: '/message/list'
        })
        this.messages = response.data
      } catch (error) {
        console.log(error)
      }
    },

    // 请求某条留言对应的评论
    async fetchComments(message_id: number) {
      try {
        const response = await axios({
          method: 'get',
          url: 'http://localhost:3000/comment/list',
          params: {
            message_id: message_id
          }
        })
        this.messages.find(
          (message) => message.message_id === message_id
        )!.comments = response.data
      } catch (error) {
        console.log(error)
      }
    },

    // 新建留言
    async createMessage(newMessage: MessageType) {
      try {
        const response = await axios({
          method: 'post',
          url: 'http://localhost:3000/message/new',
          data: newMessage
        })
        if (response.status === 1) {
          this.messages.unshift({
            ...newMessage,
            message_id: response.data.insertId,
            likes_num: 0,
            comments_num: 0
          })
          return true
        } else {
          return false
        }
      } catch (error) {
        console.log(error)
      }
    },

    // 新建评论
    async createComment(newComment: CommentType) {
      try {
        const response = await axios({
          method: 'post',
          url: 'http://localhost:3000/comment/new',
          data: newComment
        })
        if (response.status === 1) {
          const message = this.messages.find(
            (message) => message.message_id === newComment.message_id
          ) as MessageType
          if (message.comments) {
            message.comments.unshift({
              ...newComment,
              comment_id: response.data.insertId
            })
          } else {
            message.comments = [
              { ...newComment, comment_id: response.data.insertId }
            ]
          }
          message.comments_num! += 1
          return true
        } else {
          return false
        }
      } catch (error) {
        console.log(error)
      }
    },

    // 点赞 || 取消点赞
    async handleLike(option: {
      username: string
      message_id: number
      isLike: boolean
    }) {
      try {
        const response = await axios({
          method: 'post',
          url: 'http://localhost:3000/message/like',
          data: option
        })
        if (response.status === 1) {
          // 操作成功
          // 1. 修改留言信息中点赞数
          let message = this.messages.find(
            (message) => message.message_id === option.message_id
          )!
          option.isLike ? (message.likes_num! -= 1) : (message.likes_num! += 1)
          // 2. 修改用户信息中的点赞列表
          const User = useUserStore()
          option.isLike
            ? (User.user.likes = User.user.likes.filter(
                (message_id) => message_id !== option.message_id
              ))
            : User.user.likes.push(option.message_id)
          return true
        } else {
          // 操作失败
          return false
        }
      } catch (error) {
        console.log(error)
      }
    }
  }
})
